/*
 * PKL 51 GUNUNG KIDUL
 * Subseksie DATA ENTRY + CAPI
 */
package pkl51.validasi;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import javax.swing.JPanel;
import pkl51.component.TextField;
import pkl51.util.Message;
import pkl51.view.kuesioner.PanelBlok6B1;

/**
 *
 * @author Hindarwan
 */
public class ValidasiBlok6B extends AbstractValidasi {

    JPanel panelBlok6B;
    List<TextField> listTextField6B;
    int jumlahRow;

    public void setPanelBlok6B(JPanel panelBlok6B) {
        this.panelBlok6B = panelBlok6B;
    }

    public void setListTextField6B(List<TextField> listTextField6B) {
        this.listTextField6B = listTextField6B;
    }

    public ValidasiBlok6B(int jumlahRow) {
        this.jumlahRow = jumlahRow;
        listTextField6B = new LinkedList<TextField>();
        setListTextFields(listTextField6B);
    }

    public void registerValidator() {
        int index = 0;
        for (int i = 0; i < jumlahRow; i++) {
            listTextField6B.get(index++).setValidator(new ValidasiB6BC1());
            listTextField6B.get(index++).setValidator(new ValidasiB6BC2());
            listTextField6B.get(index++).setValidator(new ValidasiB6BC3());
            listTextField6B.get(index++).setValidator(new ValidasiB6BC4());
            listTextField6B.get(index++).setValidator(new ValidasiB6BC5());
        }
    }
    
    public void unRegisterValidator() {
        int index = 0;
        for (int i = 0; i < jumlahRow; i++) {
            listTextField6B.get(index++).setValidator(new NullValidasi());
            listTextField6B.get(index++).setValidator(new NullValidasi());
            listTextField6B.get(index++).setValidator(new NullValidasi());
            listTextField6B.get(index++).setValidator(new NullValidasi());
            listTextField6B.get(index++).setValidator(new NullValidasi());
        }
    }
    
    private class NullValidasi implements Validator {

        @Override
        public boolean onCheckValue(TextField field) {
            return true;
        }
    }
    
    private class ValidasiB6BC1 implements Validator {

        @Override
        public boolean onCheckValue(TextField field) {
            int index = listTextField6B.indexOf(field);
            if("0".equals(field.getText())){
                listTextField6B.get(index+1).setText("0");
                listTextField6B.get(index+1).setEnabled(false);
                listTextField6B.get(index+1).setRequired(false);
                return true;
            }
            else{
                listTextField6B.get(index+1).setEnabled(true);
                listTextField6B.get(index+1).setRequired(true);
                return true;
            }

        }
    }

    private class ValidasiB6BC2 implements Validator {

        @Override
        public boolean onCheckValue(TextField field) {
            int index = listTextField6B.indexOf(field);
            if(!"".equals(field.getText()) && "".equals(listTextField6B.get(index-1))){
                Message.errorMessage("Isi Field Banyaknya terlebih dahulu");
                return false;     
            }
            else return true;
        }
    }

    private class ValidasiB6BC3 implements Validator {

        @Override
        public boolean onCheckValue(TextField field) {
            int index = listTextField6B.indexOf(field);
            if("0".equals(field.getText())){
                listTextField6B.get(index+1).setText("0");
                listTextField6B.get(index+1).setEnabled(false);
                listTextField6B.get(index+1).setRequired(false);
                return true;
            }
            else{
                listTextField6B.get(index+1).setEnabled(true);
                listTextField6B.get(index+1).setRequired(true);
                return true;
            }
        }
    }

    private class ValidasiB6BC4 implements Validator {

        @Override
        public boolean onCheckValue(TextField field) {
            int index = listTextField6B.indexOf(field);
            if(!"".equals(field.getText()) && "".equals(listTextField6B.get(index-1))){
                Message.errorMessage("Isi Field Banyaknya terlebih dahulu");
                return false;     
            }
            else return true;
        }
    }

    private class ValidasiB6BC5 implements Validator {

        @Override
        public boolean onCheckValue(TextField field) {
            return true;
        }
    }
}
